<?php
namespace app\common\model\logic;

use think\Model;

class CmsPicModel extends Model {

    protected $name = 'cms_pic'; //定义栏目表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = true;
    //类型自动转换
    protected $type = [
        'sort'=>'integer',
        'click'=>'integer'
    ];
	//格式化列表
    public function formatList($list){
        return $list;
    }
    //格式化单条内容
    public function formatInfo($info){
        return $info;
    }
    //扩展属性读取器
    public function getAtlasArrAttr($value, $data)
    {
        $list      = explode('###', $data['atlas']);
        $imgList   = explode('|', $list[0]); //图片
        $titleList = explode('|', $list[1]); //标题
        $infoList  = explode('|', $list[2]); //描述
        foreach ($imgList as $k => $v) {
            $arr[$k]['src']   = $v;
            $arr[$k]['title'] = $titleList[$k];
            $arr[$k]['info']  = $infoList[$k];
        }
        return $arr;
    }
    protected function getContentAttr($value)
    {
        return stripslashes(htmlspecialchars_decode($value));
    }

    /**
     * 获取检索条件
     * @param $query
     */
    public function getExtendMap($query){
        return [];
    }

    //读取单条数据
    public function read($aid){
        try{
            $info = $this->get($aid)->toArray();
            return $info;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }
    //添加数据
    public function add($data){
        try{
            $this->isUpdate(false)->allowField(true)->save($data);
            return true;
        }catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }
    //修改数据
    public function edit($data){
        try{

            $this->isUpdate(true)->allowField(true)->save($data);
            return true;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }
    //删除数据
    public function del($aid){
        try{
            $this->destroy($aid);
            return true;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }
}